/*
 *  Copyright 2007 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.action.core.contentFieldValue;

import com.blandware.atleap.common.Constants;
import com.blandware.atleap.common.util.QueryInfo;
import com.blandware.atleap.model.core.ContentFieldValue;
import com.blandware.atleap.model.core.ContentLocale;
import com.blandware.atleap.service.core.ContentFieldManager;
import com.blandware.atleap.webapp.util.core.LocaleUtil;
import com.blandware.atleap.webapp.util.core.WebappConstants;
import org.apache.struts.tiles.ComponentContext;
import org.apache.struts.tiles.ControllerSupport;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;


/**
 * <p>Content field value list controller. This class is used to check can user create content field values or not.
 * </p>
 * <p><a href="ContentFieldValueListController.java.html"><i>View Source</i></a></p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.6 $ $Date: 2007/12/17 13:38:28 $
 */
public final class ContentFieldValueListController extends ControllerSupport {
	//~ Methods ================================================================
	/**
	 * Retrieves locales and puts them into request to use on the page with list of CFV
	 *
	 * @param tilesContext   Current tile context
	 * @param request        Current request
	 * @param response       Current response
	 * @param servletContext Current Servlet Context
	 */
	public void execute(ComponentContext tilesContext, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) throws Exception {

		// owner content field ID
		Long contentFieldId = (Long) request.getAttribute("contentFieldId");

		// acquire list of content field values which belong to this field
		QueryInfo queryInfo = new QueryInfo();
		Map queryParameters = new HashMap();
		queryParameters.put("contentFieldId", contentFieldId);
		queryInfo.setQueryParameters(queryParameters);
		ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
		ContentFieldManager fieldManager = (ContentFieldManager) applicationContext.getBean(Constants.CONTENT_FIELD_MANAGER_BEAN);
		List cfvList = fieldManager.listContentFieldValues(queryInfo).asList();

		// get list of enabled locales
		List enabledLocales = LocaleUtil.getInstance(request.getSession().getServletContext()).getEnabledLocales();

		// construct map
		SortedMap cfvMap = new TreeMap();
		for ( Iterator iterator = cfvList.iterator(); iterator.hasNext(); ) {
			ContentFieldValue cfv = (ContentFieldValue) iterator.next();
            if (enabledLocales.contains(cfv.getContentLocale())) {
                cfvMap.put(cfv.getContentLocale(), cfv);
            }
        }

		// add 'null' values for remaining locales
		for ( Iterator i = enabledLocales.iterator(); i.hasNext(); ) {
			ContentLocale locale = (ContentLocale) i.next();
			if ( !cfvMap.containsKey(locale) ) {
				cfvMap.put(locale, null);
			}
		}

		request.setAttribute(WebappConstants.CONTENT_FIELD_VALUES_COLLECTION_KEY, cfvMap);
	}
}
